package com.itheima.datastructure.binarysearchtree;

import com.itheima.datastructure.binarysearchtree.binarytree.TreeNode;

//查找两个节点的公共祖先
public class E07Leetcode235 {
    public TreeNode lowestCommonAncestor(TreeNode root,TreeNode p,TreeNode q){
        TreeNode a=root;//从根节点开始找
        while(p.val<a.val && q.val<a.val || p.val>a.val && q.val>a.val){ //表示两个节点同时大于a或者是小于a，在同一侧
            if(p.val<a.val){
                a=a.left;
            }else{
                a=a.right;
            }
        }
        return a;
    }
}
